package com.example.nbshopping;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nbshopping.com.example.nbshopping.login_reg.LoginRegActivity;
import com.example.nbshopping.com.example.nbshopping.login_reg.PersonCenterActivity;
import com.example.nbshopping.com.example.nbshopping.login_reg.UserBean;
import com.example.nbshopping.utils.AboutInformationActivity;
import com.example.nbshopping.utils.BaseFragment;
import com.example.nbshopping.utils.SaveUserUtils;
import com.example.nbshopping.utils.SettingActivity;


/**
 * 我的碎片
 */
public class MeFragment extends BaseFragment {
    TextView nicknameTv;
    ImageView headIv;
    Button cartBtn,historyBtn,userCenterBtn,aboutVersionBtn,settingBtn;


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        cartBtn = view.findViewById(R.id.me_btn_shoppingCart);
        historyBtn = view.findViewById(R.id.me_btn_history);
        userCenterBtn = view.findViewById(R.id.me_btn_personCenter);
        aboutVersionBtn = view.findViewById(R.id.me_btn_aboutVersion);
        settingBtn = view.findViewById(R.id.me_btn_Settings);
        headIv = view.findViewById(R.id.circleIcon);
        nicknameTv = view.findViewById(R.id.me_tv_nicknameTv);

        cartBtn.setOnClickListener(listener);
        historyBtn.setOnClickListener(listener);
        userCenterBtn.setOnClickListener(listener);
        aboutVersionBtn.setOnClickListener(listener);
        settingBtn.setOnClickListener(listener);
        headIv.setOnClickListener(listener);
    }


    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int id = view.getId();
            switch (id) {
                case R.id.me_tv_nicknameTv://点击名字，如果没登录是显示请先登录，登录是用户昵称
                    break;
                case R.id.circleIcon://点击头像，登录了显示用户头像，没登显示默认头像
                    islogin(0);
                    break;

                case R.id.me_btn_shoppingCart:
                    break;

                case R.id.me_btn_history:
                    break;

                case R.id.me_btn_personCenter:
                    islogin(0);
                    break;

                case R.id.me_btn_aboutVersion:
                    Intent it_toAbout = new Intent(getContext(), AboutInformationActivity.class);
                    //跳转页面
                    it_toAbout.putExtra("flag", 0);
                    startActivity(it_toAbout);
                    break;

                case R.id.me_btn_Settings:
                    Intent it_toSetting = new Intent(getContext(), SettingActivity.class);
                    it_toSetting.putExtra("flag", 0);
                    startActivity(it_toSetting);
                    break;
            }
        }
    };

    private void headShow() {

    }

    private void islogin(int flag) {

        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(MeFragment.super.getContext());
        Log.i("kkkkk", "islogin: user=="+userInfo);

        if (userInfo == null) {/*判断是否登录，如果登录就跳转个人中心页面，如果没有登陆，就跳转登陆页面*/


            Toast.makeText(getContext(),"您还未登录账号,请先登录",Toast.LENGTH_SHORT).show();
            Intent it_toLogin = new Intent(getContext(), LoginRegActivity.class);
            //跳转页面
            it_toLogin.putExtra("flag", flag);
            startActivity(it_toLogin);
        } else {

            Intent it_toPsnCtr = new Intent(getContext(), PersonCenterActivity.class);
            startActivity(it_toPsnCtr);

                }
            }
}